<?php
error_reporting(E_ALL);
include 'simple_html_dom.php';
$timeregexp = "/^[0-9][0-9]:[0-9][0-9]$/";
$stationregexp = "/^[0-9]+$/";
$result = array();
$bus_id = 0;
$weekarray = array("12345", "67", "1234567");
$weekdays = "12345";
$points_direct = array();
$stations = array();
$points_reverse = array();
$current_station = '';

class MyDB extends SQLite3
{
    function __construct()
    {
        $this->open('buses');
    }
}

$db = new MyDB();
$db->exec('create table if not exists routes (point_id INTEGER primary key autoincrement, bus_id INTEGER, station_id INTEGER, reverse INTEGER, time TEXT, weekdays TEXT)');
$db->exec('create table if not exists stations (station_id integer primary key autoincrement, name TEXT, inner_name TEXT)');
$db->exec("create table if not exists buses (bus_id integer primary key autoincrement, name TEXT, number TEXT, start_station INTEGER, stop_station INTEGER)");

for ($bus_id=1;($bus_id<=30);$bus_id++)
{
	$stations_made = false;
	for($i=0;$i<3;$i++)
	{
		
		$weekdays = $weekarray[$i];
		$current_bus = str_pad(strval($bus_id), 3, "0", STR_PAD_LEFT);
		print_r("Trying bus ".$current_bus." on wd ".$weekdays."\n");

		$html = file_get_html('http://www.pskovbus.ru/?pag='.$current_bus.'t1w'.$weekdays.'.html&r=1');
		
		if (($html == '') || ($html == null))
		{
			
			print_r("No such bus\n\n");
			continue;
			
		}
		$ffs = $html->find('#ffs a');
		if (count($ffs)==0)
		{
			
			print_r("No such bus\n\n");
			continue;
		}
		$points_direct = array();
		$stations = array();
		$points_reverse = array();

		foreach($ffs as $point) {
		    $href = substr($point->href, 1);
		    $station['station_id'] = $href + $bus_id*100;
		    $station['inner_name'] = $point->title;
		    $stations[] = $station;
		    $points_direct[] = $href;
		}

		foreach($html->find('#rews a') as $point) {
		    $href = substr($point->href, 1);
		    $station['station_id'] = $href + $bus_id*100;
		    $station['inner_name'] = $point->title;
		    $stations[] = $station;
		    $points_reverse[] = $href;
		}


		foreach($html->find('a[name], b, td.v4') as $point) {
		    $s = $point->plaintext;
		    if ($s=='')
		    {
			if (preg_match($stationregexp, $point->name, $matches))
			{
				$current_station = $point->name;
			}
		    }
		    if (preg_match($timeregexp, $s, $matches))
		    {    
			$item['bus_id'] = $bus_id;
			$item['station_id'] = $current_station + $bus_id*100;	
			if (in_array($current_station, $points_direct))
			{
				$item['reverse'] = 0;
			}
			if (in_array($current_station, $points_reverse))
			{
				$item['reverse'] = 1;
			}
			$item['time'] = $s;
			$item['weekdays'] = $weekdays;
			$stmt = $db->prepare('INSERT INTO routes (bus_id, station_id, reverse, time, weekdays) VALUES (:bus_id, :station_id, :reverse, :time, :weekdays)');
			foreach($item as $key => $value)
			{
				$stmt->bindValue(':'.$key, $value);
			}
			$stmt->execute();
			//print_r("\tNew point\n");
		    }
		}

		//print_r($result);
		if (!$stations_made)
		{
			$stations_made = true;
			print_r("\t\tPutting stations\n");
			foreach ($stations as $station)
			{
			
				$stations_stmt = $db->prepare('INSERT INTO stations (station_id, name, inner_name) VALUES (:station_id, :inner_name, :inner_name)');
				echo("\t\t:".$station['inner_name'].' -> '.$station['station_id']."\n");
				foreach($station as $key => $value)
				{					
					$stations_stmt->bindValue(':'.$key, $value);
				}
				$stations_stmt->execute();
			//print_r("\tNew station\n");
			}
			print_r("\t\tStations OK\n");
		}
		print_r("Next weekday\n");
	}
	print_r("=======Next bus=======\n");
}

print_r("Script OK\n");
?>